import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/authentication.dart';
import 'package:ichinesereader/controller/books.dart';
import '../../constants.dart';
import '../../controller/cache.dart';
import '../../pages/book/bookForApp.dart';
import '../../themes/appTheme.dart';
import 'package:pointer_interceptor/pointer_interceptor.dart';

class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
  CustomAppBar({Key? key, required this.title})
      : preferredSize = Size.fromHeight(48),
        // : preferredSize = Size.fromHeight(kToolbarHeight), // default is 56.0
        super(key: key);
  final String title;

  @override
  final Size preferredSize;

  final BooksController _booksController = Get.find();
  final LoginController _loginController = Get.find();

  @override
  Widget build(BuildContext context) {
    final _buildProfileBtn = IconButton(
      icon: const Icon(Icons.account_circle),
      color: Get.isDarkMode
          ? Get.theme.primaryColorDark
          : Get.theme.primaryColorLight,
      onPressed: () {
        String _userRole = box.get(CacheManagerKey['nanhairole'].toString());
        switch (_userRole) {
          case Constants.NanhaiRole_Student:
            Get.offAllNamed('/student/profile');
            break;
          case Constants.NanhaiRole_Individual:
            Get.offAllNamed('/individual/profile');
            break;
          case Constants.NanhaiRole_Teacher:
            Get.offAllNamed('/teacher/profile');
            break;
          case Constants.NanhaiRole_SchoolAdmin:
            Get.offAllNamed('/schooladmin/profile');
            break;
          case Constants.NanhaiRole_Parent:
            Get.offAllNamed('/parent/profile');
            break;
          default:
            Get.offAllNamed('/xlogin');
        }
      },
    );
    final _buildSignOutBtn = IconButton(
      icon: const Icon(Icons.logout_rounded),
      color: Get.isDarkMode
          ? Get.theme.primaryColorDark
          : Get.theme.primaryColorLight,
      onPressed: () {
        _booksController.bookFolders.clear();
        _loginController.logOut();
        Future.delayed(Duration(milliseconds: 500), () {
          Get.offAllNamed('/login');
        });
      },
    );
    final title = Text(
      this.title,
      style: Get.theme.appBarTheme.titleTextStyle?.copyWith(
        fontFamily: "Montserrat",
        fontSize: 24,
        letterSpacing: 1,
        fontWeight: FontWeight.w100,
        color: Get.isDarkMode
            ? Get.theme.primaryColorDark
            : Get.theme.primaryColorLight,
      ),
      overflow: TextOverflow.fade,
      softWrap: false,
    );

    return Stack(
      alignment: Alignment.center,
      children: [
        AppBar(
          backgroundColor: Get.isDarkMode
              ? AppTheme.colors.darkTheme2
              : Get.theme.primaryColorDark.withOpacity(0.8),
          title: title,
          elevation: 9,
          actions: <Widget>[
            //SettingsBarHorizontal(true),
            PointerInterceptor(child: _buildProfileBtn),
            PointerInterceptor(child: _buildSignOutBtn),
          ],
        ),
        //
      ],
    );
  }
}
